home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
WINMX Assorted Textfiles
/
Ebooks.tar
/
Text - Tech - Programming - Visual Basic - Nod Programing VB Help Index 4 (TXT).zip
/
VBTips4.txt
Wrap
Text File
|
1998-04-09
|
11KB
|
333 lines
Nod Programming Inc. VB Help Index
»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
This is intended for free use. The code here is for various skill levels,
anyone from beginers to advanced programers can use these. Do what you wish
with the code it is free for you to use and manipulate!
****************************************************************************
Creating Win32 region windows:
The Win32 API includes a really amazing feature called region windows. A
window under Win32 no longer has to be rectangular! In fact, it can be any
shape that may be constructed using Win32 region functions. Using the
SetWindowRgn Win32 function from within VB is so simple, but the results
are unbelievable! The following example shows a VB form that is NOT
rectangular!!
Here is the code. Enjoy!
' This goes into the General Declarations section:
Private Declare Function CreateEllipticRgn Lib "gdi32" _
(ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" _
(ByVal hWnd As Long, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Private Sub Form_Load()
Show 'The form!
SetWindowRgn hWnd, _
CreateEllipticRgn(0, 0, 300, 200), _
True
End Sub
****************************************************************************
Manipulate your controls from the keyboard:
If you're not comfortable using your mouse--or can't achieve the precise
results you'd like--these tips will come in handy.
First, you can resize controls at design time by using the [Shift] and
arrow keys, as follows:
SHIFT + RIGHT ARROW increases the width of the control
SHIFT + LEFT ARROW decreases the width of the control
SHIFT + DOWN ARROW increases the height of the control
SHIFT + UP ARROW decreases the height of the control
Note: The target control must have focus, so click on the control before
manipulating it from the keyboard.
Second, by using the [Control] key and the arrow keys, you can move your
controls at design time, as follows:
CONTROL + RIGHT ARROW to move the control to the right
CONTROL + LEFT ARROW to move the control to the left
CONTROL + DOWN ARROW to move the control downwards
CONTROL + UP ARROW to move the control upwards
If you select more than one control (by clicking on the first and
shift-clicking on the others), the above procedures will affect all the
selected controls.
****************************************************************************
Simple file checking from anywhere:
To keep my applications running smoothly, I often need to check that
certain files exist. So, I've written a simple routine to make sure they
do. Here it is:
Public Sub VerifyFile(FileName As String)
'
On Error Resume Next
'Open a specified existing file
Open FileName For Input As #1
'Error handler generates error message with file and exits the routine
If Err Then
MsgBox ("The file " & FileName & " cannot be found.")
Exit Sub
End If
Close #1
'
End Sub
Now add a button to your form and place the code below behind the "Click"
event.
Private Sub cmdVerify_Click()
'
Call VerifyFile("MyFile.txt")
'
End Sub
****************************************************************************
Dragging items from one list to another:
Here's a way that you can let users drag items from one list and drop them
in another one.
Create two lists (lstDraggedItems, lstDroppedItems) and a text box
(txtItem) in a form (frmTip).
Put the following code in the load event of your form.
Private Sub Form_Load()
' Set the visible property of txtItem to false
txtItem.Visible = False
'Add items to list1 (lstDraggedItems)
lstDraggedItems.AddItem "Apple"
lstDraggedItems.AddItem "Orange"
lstDraggedItems.AddItem "Grape"
lstDraggedItems.AddItem "Banana"
lstDraggedItems.AddItem "Lemon"
'
End Sub
In the mouseDown event of the list lstDraggedItems put the following code:
Private Sub lstDraggedItems_MouseDown(Button As Integer, Shift As Integer,
X As Single, Y As Single)
'
txtItem.Text = lstDraggedItems.Text
txtItem.Top = Y + lstDraggedItems.Top
txtItem.Left = X + lstDraggedItems.Left
txtItem.Drag
'
End Sub
In the dragDrop event of the list lstDroppedItems put the following code:
Private Sub lstDroppedItems_DragDrop(Source As Control, X As Single, Y As
Single)
'
If lstDraggedItems.ItemData(lstDraggedItems.ListIndex) = 9 Then
Exit Sub
End If
' To make sure that this item will not be selected again
lstDraggedItems.ItemData(lstDraggedItems.ListIndex) = 9
lstDroppedItems.AddItem txtItem.Text
'
End Sub
Now you can drag items from lstDraggedItems and drop them in=
LstDroppedItems.
Note that you cannot drag from the second list to the first. Also, the
dragged item remains in the first list. You'll have to address those
limitations yourself.
****************************************************************************
Creating a new context menu in editable controls:
This routine will permit you to replace the original context menu with your
private context menu in an editable control.
Add the following code to your form or to a BAS module:
Private Const WM_RBUTTONDOWN = &H204
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long
Public Sub OpenContextMenu(FormName As Form, MenuName As Menu)
'Tell system we did a right-click on the mdi
Call SendMessage(FormName.hwnd, WM_RBUTTONDOWN, 0, 0&)
'Show my context menu
FormName.PopupMenu MenuName
'
End Sub
Next, use the Visual Basic Menu Editor and the table below to create a
simple menu.
Caption Name Visible
Context Menu mnuContext NO
...First Item mnuContext1
...Second Item mnuContext2
Note that the last two items in the menu are indented (...) one level and
that only the first item in the list ("Context Menu") has the Visible
property set to NO.
Now add a text box to your form and enter the code below in the MouseDown
event of the text box.
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
If Button = vbRightButton Then
Call OpenContextMenu(Me, Me.mnuContext)
End If
End Sub
Note: If you just want to kill the system context menu, just comment out
the line:
FormName.PopupMenu MenuName
in the OpenContextMenu routine.
****************************************************************************
Quick Custom Dialogs for DBGrid Cells:
It's easy to add custom input dialogs to al the cells in the Microsoft Data
Bound Grid control.
First, add a DBGrid control and Data control to your form. Next, set the
DatabaseName and RecordSource properties of the data control to a valid
database and table ("biblio.mdb" and "Publishers" for example). Then set
the DataSource property of the DBGrid control to Data1 (the data control).
Now add the following code to your form.
' general declaration area
Dim strDBGridCell As String
Private Sub DBGrid1_AfterColEdit(ByVal ColIndex As Integer)
'
DBGrid1.Columns(ColIndex) = strDBGridCell
'
End Sub
Private Sub DBGrid1_BeforeColEdit(ByVal ColIndex As Integer, ByVal KeyAscii
As Integer, Cancel As Integer)
'
strDBGridCell = InputBox("Edit DBGrid Cell:", ,=
DBGrid1.Columns(ColIndex))
'
End Sub
Now whenever you attempt to edit any cell in the DBGrid, you'll see the
InputBox prompt you for input. You can replace the InputBox with any other
custom dialog you wish to build.
****************************************************************************
Using the Alias Option to Prevent API Crashes:
A number of Windows APIs have parameters that can be more than one data
type. For example, the WinHelp API call can accept the last parameter as a
Long or String data type depending on the service requested.
Visual Basic allows you to declare this data type as "Any" in the API call,
but this can lead to type mismatch errors or even system crashes if the
value is not the proper form.
You can prevent the errors and improve the run-time type checking by
declaring multiple versions of the same API function in your program. By
adding a function declaration for each possible parameter type, you can
continue to use strong data type checking.
To illustrate this technique, add the following APIs and constants to a
Visual Basic form. Notice that the two API declarations differ only in
their initial name ("WinHelp" and "WinHelpSearch") and the type declaration
of the last parameter ("dwData as Long" and "dwData as String").
' WinHelp APIs
Private Declare Function WinHelp Lib "user32" Alias "WinHelpA" (ByVal hwnd
As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal dwData
As Long) As Long
Private Declare Function WinHelpSearch Lib "user32" Alias "WinHelpA" (ByVal
hwnd As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal
dwData As String) As Long
'
Private Const HELP_PARTIALKEY = &H105&
Private Const HELP_HELPONHELP = &H4
Private Const HelpFile = "c:\program files\devstudio\vb5\help\vb5.hlp"
Now add two command buttons to your form (cmdHelpAbout and cmdHelpSearch)
and place the following code behind the buttons. Be sure to edit the
location of the help file to match your installation of Visual Basic.
Private Sub cmdHelpAbout_Click()
'
WinHelp Me.hwnd, HelpFile, HELP_HELPONHELP, &H0
'
End Sub
Private Sub cmdHelpSearch_Click()
'
WinHelpSearch Me.hwnd, HelpFile, HELP_PARTIALKEY, "option"
'
End Sub
When you press on the HelpAbout button, you'll see help about using the
help system. When you press on the HelpSearch button, you'll see a list of
help entries on the "option" topic.
****************************************************************************
Add Dithered Backgrounds to your VB Forms:
Ever wonder how the SETUP.EXE screen gets its cool shaded background
coloring? This color shading is called dithering, and you can easily
incorporate it into your forms. Add the following routine to a form:
Sub Dither(vForm As Form)
Dim intLoop As Integer
vForm.DrawStyle = vbInsideSolid
vForm.DrawMode = vbCopyPen
vForm.ScaleMode = vbPixels
vForm.DrawWidth = 2
vForm.ScaleHeight = 256
For intLoop = 0 To 255
vForm.Line (0, intLoop)-(Screen.Width, intLoop - 1), RGB(0, 0,
255 -intLoop), B
Next intLoop
End Sub
Now, add to the Form_Activate event the line
Dither ME
This version creates a fading blue background by adjusting the blue value
in the RGB function. (RGB stands for Red-Green-Blue.) You can create a
fading red background by changing the RGB call to
RGB(255 - intLoop, 0, 0).
****************************************************************************
End of Help 4 of how many I do!!